package com.authorityManagement.service.impl;

import com.authorityManagement.entity.LoginUser;
import com.authorityManagement.entity.Menu;
import com.authorityManagement.entity.User;
import com.authorityManagement.exception.CustomerExceptionHandler;
import com.authorityManagement.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
      
      @Autowired
      private UserMapper userMapper;
      
      @Override
      public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            User user = userMapper.selectOne(new LambdaQueryWrapper<User>()
                  .eq(User::getUserName, username));
            if (user == null) {
                  throw new CustomerExceptionHandler("用户不存在,请重新输入用户");
            }
      
            List<Menu> authorityList = userMapper.queryAllAuthorityList(user.getId());
            
            return new LoginUser(user, authorityList);
      }
}
